package com.xrtn.service.impl;

import com.xrtn.bean.*;
import com.xrtn.mapper.*;
import com.xrtn.service.IBookingService;
import com.xrtn.service.ICommonService;
import com.xrtn.utils.DateUtils;
import com.xrtn.utils.HttpUtils;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.SendValidataCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.Oneway;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tu_pei on 2018/3/5.
 */
@Service
public class BookingServiceImpl implements IBookingService {

    @Autowired
    private TBookingInfoMapper bookingInfoMapper;

    @Autowired
    private TPatientInfoMapper patientInfoMapper;

    @Autowired
    private TPatientTreatmentMapper patientTreatmentMapper;

    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Autowired
    private TSubclinicInfoMapper subclinicInfoMapper;

    @Autowired
    private TUserInfoMapper userInfoMapper;

    @Override
    public List<Map<String, Object>> selectDoctorBookingList(Map<String, Object> params) {
        return bookingInfoMapper.selectDoctorBookingList(params);
    }

    /**
     * 修改预约状态
     *
     * @param bookingInfo
     * @return
     */
    @Override
    public Integer updateBooking(TBookingInfo bookingInfo) {

        TBookingInfo bookingInfos = bookingInfoMapper.selectByPrimaryKey(bookingInfo.getBookingId());
        bookingInfo.setCancelTime(new Date());
        bookingInfo.setCancelType(2);
        if (ObjUtils.isEmpty(bookingInfos)) {
            return 0;
        }

        TUserInfo userInfo = userInfoMapper.selectByPrimaryKey(bookingInfos.getUserId());
        SysUserInfo sysUserInfo = sysUserInfoMapper.selectByPrimaryKey(bookingInfos.getDoctorId());
        TSubclinicInfo subclinicInfo = subclinicInfoMapper.selectByPrimaryKey(bookingInfos.getSubclinicId());
        TPatientInfo patient = patientInfoMapper.selectByPrimaryKey(bookingInfos.getPatientId());

        ICommonService.cancleBookingPushAndMessage(bookingInfos, userInfo, sysUserInfo, subclinicInfo, patient);

        return bookingInfoMapper.updateByPrimaryKeySelective(bookingInfo);
    }

    /**
     * 获取预约详情
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> selectBookingInfo(Map<String, Object> params) {

        Map<String, Object> bookingMap = bookingInfoMapper.selectBookingInfo(params);
        TPatientTreatment treatment = patientTreatmentMapper.selectByMap(params);

        if(ObjUtils.isNotEmpty(treatment)){
            bookingMap.put("treatmentId" , treatment.getTreatmentId());
        }

        return bookingMap;
    }

    /**
     * 获取预约详情
     *
     * @param bookingInfo
     * @return
     */
    @Override
    public Integer updateBookingInfo(TBookingInfo bookingInfo) {

        //取消预约
        TBookingInfo booking = bookingInfoMapper.selectByPrimaryKey(bookingInfo.getBookingId());

        if(ObjUtils.isNotEmpty(booking)){
            TUserInfo userInfo = userInfoMapper.selectByPrimaryKey(bookingInfo.getUserId());
            SysUserInfo sysUserInfo = sysUserInfoMapper.selectByPrimaryKey(bookingInfo.getDoctorId());
            TSubclinicInfo subclinicInfo = subclinicInfoMapper.selectByPrimaryKey(bookingInfo.getSubclinicId());
            TPatientInfo patient = patientInfoMapper.selectByPrimaryKey(bookingInfo.getPatientId());

            ICommonService.cancleBookingPushAndMessage(booking, userInfo, sysUserInfo, subclinicInfo, patient);
        }
        return bookingInfoMapper.updateByPrimaryKeySelective(bookingInfo);
    }

    public static void main(String args[]){
        Map<String,String> params = new HashMap<>();
//        params.put("openId", "o5u9Y1KjKj_E1mxgewii3LZ6Y_c0");
//        params.put("doctorName", "test_doctor");
//        params.put("bookingTime", "2018-07-02");
//        params.put("subclinicName", "祥云");
//        params.put("orderNo", "2111212");
        //String str = HttpUtils.submitPostData("http://wechat.xrtn.dmpp.net.cn/children/frontend/web/index.php?r=wechat/sendcancelbooking", params,"utf-8");
        params.put("openId", "o5u9Y1KjKj_E1mxgewii3LZ6Y_c0");
        params.put("subclinicName", "123");
        params.put("bookingTime", "2018-07-02");
        params.put("orderNo", "123");
        String str = HttpUtils.submitPostData("http://wechat.xrtn.dmpp.net.cn/children/frontend/web/index.php?r=wechat/sendorderpaysuccess", params,"utf-8");

        System.out.println("str = " + str);
    }
}
